<?php

namespace App\Models\FeishuCallingCard;

use App\Models\BaseModel;
use App\Service\CallingCards\ShareImageService;
use App\Service\CallingCards\WechatService;
use App\Service\Common\OssService;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Tymon\JWTAuth\Contracts\JWTSubject;

class FeishuCallingCard extends BaseModel implements AuthenticatableInterface, JWTSubject
{
    use Authenticatable, HasFactory;

    protected $table = 'feishu_calling_cards';

    protected $fillable = [
        'feishuId',
        'name',
        'mobile',
        'position',
        'companyAddressId',
        'wechatId',
        'avatar',
        'wechatUrl',
        'wxShareImg',
    ];

    //是否显示职位
    const SHOW_POSITION_ON  = 1;
    const SHOW_POSITION_OFF = 0;

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [
            'modelType' => static::class,
            'name' => $this->name,
        ];
    }

    /**
     * 更新用户分享用的微信二维码
     * @return string
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function refreshWechatQrcode()
    {
        try {
            $arr = ShareImageService::shareImage($this);
            $this->attributes['wechatQrcode'] = $arr['path'] ?? '';
        } catch (\Exception $e) {
            \ExceptionNotifier::report($e);
        }
        return $this->attributes['wechatQrcode'];
    }

    public function getWechatQrcodeAttribute()
    {
        if (!strlen($this->attributes['wechatQrcode'] ?? '')) {
            $this->refreshWechatQrcode();
            $this->save();
        }
        return $this->attributes['wechatQrcode'];
    }

    public function getOriginalCodeAttribute()
    {
        if (0 == strlen($this->attributes['originalCode'] ?? '') && $this->id) {
            try {
                $content = WechatService::genCallingQrCode($this->id);
                $tmpFile = sys_get_temp_dir() . '/' . Str::random(10) . '.png';
                file_put_contents($tmpFile, $content);
                Image::make($tmpFile)
                    ->resize(248, 248)
                    ->save($tmpFile);
                $arr = OssService::uploadFile($tmpFile);
                $this->attributes['originalCode'] = $arr['path'] ?? '';
                $this->save();
                @unlink($tmpFile);
            } catch (\Exception $e) {}
        }
        return $this->attributes['originalCode'];
    }

    /*public function getDepartmentNameAttribute()
    {
        try {
            $name = $this->attributes['departmentName'] ?? '';
            if (!strlen($name)) {
                //获得用户信息
                $userInfo = FeishuApiService::getUserInfoByFeishuId($this->feishuId);
                if (!is_array($userInfo['data']['user'] ?? null)) {
                    throw new \Exception('飞书用户信息获取失败');
                }
                //根据用户信息的部门ID获得一级部门名称
                $departmentName = FeishuApiService::getDepartmentName($userInfo['data']['user']['department_ids'] ?? []);
                // diao接口
                $this->attributes['departmentName'] = $departmentName;
                $this->save();
            }
            return $name;
        } catch (\Exception $e) {}
    }*/
}
